package com.acce.system.service.impl;

import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.acce.base.exception.ApplicationException;
import com.acce.base.pojo.ArgsPojo;
import com.acce.base.pojo.ReturnCode;
import com.acce.base.pojo.ReturnPojo;
import com.acce.base.utils.Constants;
import com.acce.base.utils.EncodingTool;
import com.acce.base.utils.ErrCode;
import com.acce.base.utils.ErrMsg;
import com.acce.base.utils.TokenUtils;
import com.acce.base.utils.TwoDimensionCode;
import com.acce.base.utils.Utils;
import com.acce.product.dao.IBuyGoodsDao;
import com.acce.product.dao.IGoodsDao;
import com.acce.product.pojo.BuyGoods;
import com.acce.product.pojo.Goods;
import com.acce.system.config.ReturnCodes;
import com.acce.system.dao.IBusinessLicenseDao;
import com.acce.system.dao.ICompanyDao;
import com.acce.system.dao.ICompanyPropDao;
import com.acce.system.dao.ICompanyRelationshipDao;
import com.acce.system.dao.IDeliverAddressDao;
import com.acce.system.dao.IDepartmentDao;
import com.acce.system.dao.IEmployeeInfoDao;
import com.acce.system.dao.ILogDao;
import com.acce.system.dao.ILoginLogDao;
import com.acce.system.dao.IQrScanDao;
import com.acce.system.dao.IReturnCodeDao;
import com.acce.system.dao.ITokenConfigDao;
import com.acce.system.dao.IUserDao;
import com.acce.system.dao.IUserPasswordDao;
import com.acce.system.dao.IUserPropDao;
import com.acce.system.pojo.BusinessLicense;
import com.acce.system.pojo.CompanyDepartment;
import com.acce.system.pojo.CompanyList;
import com.acce.system.pojo.CompanyProp;
import com.acce.system.pojo.CompanyRelationship;
import com.acce.system.pojo.DeliverAddress;
import com.acce.system.pojo.EmployeeInfo;
import com.acce.system.pojo.LoginLog;
import com.acce.system.pojo.QrScan;
import com.acce.system.pojo.UserList;
import com.acce.system.pojo.UserPassword;
import com.acce.system.pojo.UserProp;
import com.acce.system.service.IUserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiniu.util.Auth;

@Service("userService")
public class UserServiceImpl implements IUserService {
	private String preMod = "nsy-auth";
	private static Logger logger = Logger.getLogger(UserServiceImpl.class);
	
	@Resource
	private IUserDao userDao;
	
	@Resource
	private IUserPropDao userPropDao;
	
	@Resource
	private ILoginLogDao loginLogDao;
	
	@Resource
	private ILogDao operDao;
	
	@Resource
	private IQrScanDao qrScanDao;
	
	@Resource
	private ICompanyDao companyDao;
	
	@Resource
	private ICompanyPropDao companyPropDao;
	
	@Resource
	private IDepartmentDao departmentDao;
	
	@Resource
	private ITokenConfigDao tokenConfigDao;
	
	@Resource
	private IUserPasswordDao userPasswordDao;
	
	@Resource
	IBuyGoodsDao buyGoodsDao;
	
	@Resource
	private ICompanyRelationshipDao companyRelationshipDao;
	
	@Resource
	private IEmployeeInfoDao employeeInfoDao;
	
	@Resource
	private IGoodsDao goodsDao;
	
	@Resource
	private IBusinessLicenseDao businessLicenseDao;
	
	@Resource
	private IDeliverAddressDao deliverAddressDao;
	@Resource
	private IReturnCodeDao returnCodeDao;
	
//	@Override
//	public boolean insertNotify(ArgsPojo args) {
//		AccessNotify an = new AccessNotify();
//		an.setNotifyId(args.getNotifyId());
//		an.setUpdateUser(args.getAtUserId());
//		an.setUpdateTime(Utils.getCurrTime());
//		return userDao.insertNotify(an) > 0 ? true : false;
//	}

	public boolean createQrUid(String uuid) {
		return qrScanDao.createQrUid(uuid) > 0 ? true : false;
	}

	public boolean updateScanQr(QrScan qrScan) {
		return qrScanDao.updateScanQr(qrScan) > 0 ? true : false;
	}

	public QrScan scanFlag(String uid) {
		return qrScanDao.scanFlag(uid);
	}
	
	/**
	 * 账号查重
	 */
	public ReturnPojo checkNumRepeat(ArgsPojo args) {
		String locMe = preMod + "::checkNumRepeat";
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		logger.info(locMe + "::business::" + business);
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String registerNum = jb.getString("registerNum");
		String registerType = jb.getString("registerType");
		if(StringUtils.isEmpty(registerNum) || StringUtils.isEmpty(registerType)){
			logger.info(locMe + "::missing args: [registerNum, registerType]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		logger.info(locMe + "::begin checkNum::"  + registerNum);
		UserList ul = new UserList();
		boolean isPhone = false;
		if("p".equals(registerType.toLowerCase())){
			ul.setAtUserId(registerNum);
			isPhone = true;
		}else if("q".equals(registerType.toLowerCase())){
			ul.setQqNum(registerNum);
		}else if("w".equals(registerType.toLowerCase())){
			ul.setWeChatNum(registerNum);
		}else if("e".equals(registerType.toLowerCase())){
			ul.setEmailAddr(registerNum);
		}
		UserList ac = null;
		logger.info(locMe + "::isPhone::"  + isPhone);
		if(isPhone){ //是手机号则查询主键
			try {
				ac = userDao.getUserById(ul);
			} catch (Exception e) {
				logger.info(locMe + "::get user by ID error::"  + e.getMessage());
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.QUERY_FAIL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
		}else{ //其他账号
			List<UserList> list = userDao.getUserByCondition(ul);
			if(null == list || list.size() == 0){
				ac = null;
			}else if(list.size() == 1){
				ac = list.get(0);
			}else if(list.size() > 1){
				logger.info(locMe + "::too many user");
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOO_MANY_DATA.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
		}
		logger.info(locMe + "::user is" + ac);
		if(ac == null){ 
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{ 
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_ACCOUNT.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}

	/**
	 * 个人注册
	 */
	@Transactional
	public ReturnPojo registerUser(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String registerNum = jb.getString("registerNum");
		String registerType = jb.getString("registerType");
		String realName = jb.getString("realyName");
		String companyType = jb.getString("companyType"); ;
		String pwd = jb.getString("pwd");
		String isWX = jb.getString("isWX");
		String openId = jb.getString("openId");
		String code = jb.getString("code");
		String editUserId = args.getEditUserId();
		String editTime = args.getEditTime();
		if(StringUtils.isEmpty(registerNum) || StringUtils.isEmpty(registerType) || StringUtils.isEmpty(realName) 
				|| StringUtils.isEmpty(companyType) || StringUtils.isEmpty(pwd) ){
			logger.info("nsy-auth::registerUser:: missing args: [registerNum,registerType,realName,companyType,pwd]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		//验证短信验证码
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("appkey", "15eeda6c40e20");
//		params.put("phone", registerNum);
//		params.put("zone", "86");
//		params.put("code", code);
//		logger.info("nsy-core::registerUser::params :" + params);
//		String resultString = Utils.sendhttp("https://webapi.sms.mob.com/sms/verify", params);
//		logger.info("nsy-core::registerUser::resultString :" + resultString);
//		JSONObject returnObject = JSON.parseObject(resultString);
//		String status = returnObject.getString("status");
//		if(!"200".equals(status)){
//			//错误提示
//			logger.error("nsy-auth::registerUser::code valid error::");
//			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.CODE_VALID_ERROR.getCode());
//			rp.setReturnCode(rc.getCode());
//			rp.setReturnMsg(rc.getMessage());
//			return rp;
//		}
		//插入用user
		UserList user = new UserList();
		//查重用user
		UserList ul = new UserList();
		boolean isPhone = false;
		if("p".equals(registerType.toLowerCase())){
			ul.setAtUserId(registerNum);
			user.setPhoneNum(registerNum);
			isPhone = true;
		}else if("q".equals(registerType.toLowerCase())){
			ul.setQqNum(registerNum);
			user.setQqNum(registerNum);
		}else if("w".equals(registerType.toLowerCase())){
			ul.setWeChatNum(registerNum);
			user.setWeChatNum(registerNum);
		}else if("e".equals(registerType.toLowerCase())){
			ul.setEmailAddr(registerNum);
			user.setEmailAddr(registerNum);
		}
		UserList ac = null;
		if(isPhone){
			try {
				ac = userDao.getUserById(ul);
			} catch (Exception e) {
				logger.error("nsy-auth::registerUser::get user by ID error::" + e.getMessage());
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_ACCOUNT.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
		}else{
			List<UserList> list = userDao.getUserByCondition(ul);
			if(null == list || list.size() == 0){
				ac = null;
			}else if(list.size() == 1){
				ac = list.get(0);
			}else if(list.size() > 1){
				logger.error("nsy-auth::registerUser::too many user");
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_ACCOUNT.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
		}
		if(ac == null){ //账号不存在，可注册
			String currTime = Utils.getCurrTimeMillions();
			String atUserId = registerNum;
			String companyId = registerNum;
			String companyName = realName + "@" + registerNum;
			user.setAtUserId(atUserId);
			user.setRealName(realName);
			user.setEditUserId(editUserId);
			user.setEditTime(editTime);
			user.setSyncTime(currTime);
			user.setSyncType("0");
			user.setCreateTime(currTime);
			user.setValidity("1");
			//准备插入密码表
			UserPassword password = new UserPassword();
			password.setUserPwd(pwd);
			password.setAuthenicantion("MD5");
			password.setSignType("SHA-1");
			password.setAtUserId(atUserId);
			password.setValidity("1");
			if(!StringUtils.isEmpty(isWX) && "yes".equals(isWX)){
				if(!StringUtils.isEmpty(openId) && !"null".equals(openId)){
					password.setOpenId(openId);
				}
			}
			password.setEditUserId(editUserId);
			password.setEditTime(editTime);
			password.setSyncTime(currTime);
			password.setSyncType("0");
			password.setCreateTime(currTime);
			userDao.insertUser(user);
			logger.info("nsy-user::register add user end ...");
			userPasswordDao.insertUserPassword(password);
			logger.info("nsy-user::register add user password end ...");
			
			try {
				File file = new File("");
				if(!file.exists()){
					file.mkdirs();
				}
				String qrCodePath = "" + File.separator + atUserId + ".png";
				TwoDimensionCode.makeQRCode(realName, qrCodePath);
			} catch (Exception e1) {
				logger.error("nsy-user::register::make rqcode error::" + e1.getMessage());
				e1.printStackTrace();
				throw new ApplicationException("生成二维码失败");
			}
			//初始化prop
			UserProp prop = new UserProp();
			prop.setAtUserId(atUserId);
			prop.setPropName("qrCode");
			prop.setPropValue(Constants.USER_SERVICE_ADDR + "/upload/" + atUserId + ".png");
			prop.setEditUserId(editUserId);
			prop.setEditTime(editTime);
			prop.setSyncTime(currTime);
			prop.setSyncType("0");
			prop.setCreateTime(currTime);
			List<UserProp> props = new ArrayList<UserProp>();
			props.add(prop);
			userPropDao.insertBatchUserProp(props);
			logger.info("nsy-user::register add user prop end ...");
			
			if(!"0".equals(companyType)) { //同时注册公司
				CompanyList company = new CompanyList();
				company.setCompanyId(companyId);
				company.setCompanyName(companyName);
				company.setCompanyType(companyType);
				company.setFounderId(registerNum);
				company.setFounderName(realName);
				company.setAdminId(registerNum);
				company.setAdminName(realName);
				company.setIndividual("1");
				company.setValidity("1");
				company.setEditTime(editTime);
				company.setEditUserId(editUserId);
				company.setSyncTime(currTime);
				company.setSyncType("0");
				company.setCreateTime(currTime);
				companyDao.insertCompany(company);
				logger.info("nsy-user::registerUser::add company end ...");
				//初始化工商信息
				BusinessLicense blice = new BusinessLicense();
				blice.setCompanyId(companyId);
				blice.setValidity("1");
				blice.setEditUserId(editUserId);
				blice.setEditTime(editTime);
				blice.setCreateTime(currTime);
				blice.setSyncTime(currTime);
				blice.setSyncType("0");
				businessLicenseDao.insertBusinessLicense(blice);
				logger.info("nsy-user::registerUser add businessLicense end ...");
				String departmentId = "";
				String departmentName = "";
				if("100".equals(companyType)) {//购销商
					CompanyDepartment dept = new CompanyDepartment();
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setType("1");
					dept.setFatherId(companyId);//0为根目录
					dept.setFatherName(companyName);
					dept.setSonDepart("经营部");//本级部门
					dept.setLevelNum("1");//层级一级
					dept.setValidity("1");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("销售部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("采购部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("财务部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("库房管理部");
					departmentDao.insertDepartment(dept);
					
					String defultId = Utils.makeDepartmentId();
					dept.setDepartmentId(defultId);
					dept.setSonDepart("农商网络管理部");
					departmentDao.insertDepartment(dept);
					
					departmentId = Utils.makeDepartmentId();
					departmentName = "网络管理部";
					dept.setDepartmentId(departmentId);
					dept.setType("2");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setFatherId(defultId);
					dept.setFatherName("农商网络管理部");
					dept.setSonDepart("网络管理员");
					dept.setLevelNum("30");
					dept.setValidity("1");
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
					logger.info("nsy-user::register add 100 department end ...");
				} else if("200".equals(companyType)) {//餐饮企业
					CompanyDepartment dept = new CompanyDepartment();
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setFatherId(companyId);
					dept.setFatherName(companyName);
					dept.setSonDepart("财务部");//本级部门
					dept.setLevelNum("1");//层级一级
					dept.setValidity("1");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("收银部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("采购部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("厨房管理部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("前厅管理部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("库房管理部");
					departmentDao.insertDepartment(dept);
					
					String defaulId = Utils.makeDepartmentId();
					dept.setDepartmentId(defaulId);
					dept.setSonDepart("农商网络管理部");
					departmentDao.insertDepartment(dept);
					
					departmentId = Utils.makeDepartmentId();
					departmentName = "网络管理部";
					dept.setDepartmentId(departmentId);
					dept.setType("2");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setFatherId(defaulId);
					dept.setFatherName("农商网络管理部");
					dept.setSonDepart("网络管理员");
					dept.setLevelNum("30");
					dept.setValidity("1");
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
					logger.info("nsy-user::register add 200 department end ...");
				}else{
					throw new ApplicationException("company type error " + companyType);
				}
				logger.info("nsy-user::registerCompany add department end ...");
				//公司关联员工
				EmployeeInfo emp = new EmployeeInfo();
				emp.setAtUserId(atUserId);
				emp.setRealName(realName);
				emp.setCompanyId(companyId);
				emp.setCompanyName(companyName);
				emp.setDepartmentId(departmentId);
				emp.setDepartmentName(departmentName);
				emp.setPosition("网络管理员");
				emp.setValidity("1");
				emp.setOpAuthority("1");
				emp.setCreater("1");
				emp.setAuditIsPassed("1");
				emp.setSyncTime(currTime);
				emp.setSyncType("0");
				emp.setEditUserId(editUserId);
				emp.setEditTime(editTime);
				emp.setCreateTime(currTime);
				employeeInfoDao.insertEmployeeInfo(emp);
				logger.info("nsy-user::registerCompany add employee end ...");
				//初始化商品
				List<BuyGoods> bgs = new ArrayList<BuyGoods>();
				Goods g = new Goods();
				if("100".equals(companyType)){
					g.setRawSelect("1");
					List<Goods> list = goodsDao.getGoodsByCondition(g);
					for(int i = 0, size = list.size(); i < size; i++){
						g = list.get(i);
						BuyGoods bg = new BuyGoods();
						bg.setBuyGoodsId(Utils.makeProductId());
						bg.setCompanyId(companyId);
						bg.setCompanyName(companyName);
						bg.setCompanyType(companyType);
						bg.setOpAuthority(g.getOpAuthority());
						bg.setValidity(g.getValidity());
						bg.setCommodity(g.getCommodity());
						bg.setFather(g.getFather());
						bg.setSon(g.getSon());
						bg.setLevelNum(g.getLevelNum());
						bg.setProvince(g.getProvince());
						bg.setCity(g.getCity());
						bg.setCompanyDefaultState(g.getCompanyDefaultState());
						bg.setProQuantity(g.getProQuantity());
						bg.setGoodsKingdom(g.getGoodsKingdom());
						bg.setUnit(g.getUnit());
						bg.setSelected("0");
						bg.setSequenceNum(g.getSequenceNum());
						bg.setEditUserId(editUserId);
						bg.setEditTime(editTime);
						bg.setSyncType("0");
						bg.setSyncTime(currTime);
						bg.setCreateTime(currTime);
						bgs.add(bg);
					}
				}else if("200".equals(companyType)){
					g.setSaleSelect("1");
					List<Goods> list = goodsDao.getGoodsByCondition(g);
					for(int i = 0, size = list.size(); i < size; i++){
						g = list.get(i);
						BuyGoods bg = new BuyGoods();
						bg.setBuyGoodsId(Utils.makeProductId());
						bg.setCompanyId(companyId);
						bg.setCompanyName(companyName);
						bg.setCompanyType(companyType);
						bg.setOpAuthority(g.getOpAuthority());
						bg.setValidity(g.getValidity());
						bg.setCommodity(g.getCommodity());
						bg.setFather(g.getFather());
						bg.setSon(g.getSon());
						bg.setLevelNum(g.getLevelNum());
						bg.setProvince(g.getProvince());
						bg.setCity(g.getCity());
						bg.setCompanyDefaultState(g.getCompanyDefaultState());
						bg.setProQuantity(g.getProQuantity());
						bg.setGoodsKingdom(g.getGoodsKingdom());
						bg.setUnit(g.getUnit());
						bg.setSelected("0");
						bg.setSequenceNum(g.getSequenceNum());
						bg.setEditUserId(editUserId);
						bg.setEditTime(editTime);
						bg.setSyncType("0");
						bg.setSyncTime(currTime);
						bg.setCreateTime(currTime);
						bgs.add(bg);
					}
				}
				try {
					buyGoodsDao.insertBatchBuyGoods(bgs);
				} catch (Exception e) {
					logger.info("nsy-user::register::insertBatchBuyGoods error::" + e.getMessage());
				}
				
				logger.info("nsy-user::register init goods end ...");
			}
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{ 
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_ACCOUNT.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 公司名查重
	 */
	public ReturnPojo checkCompanyRepeat(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyName = jb.getString("companyName");
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyName)){
			logger.info("nsy-auth::checkCompanyRepeat:: missing args: [atUserId, companyName]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		CompanyList cl = new CompanyList();
		cl.setCompanyName(companyName);
		List<CompanyList> list = companyDao.getCompanyByCondition(cl);
		if(null == list || list.size() == 0){
			cl = null;
		}else if(list.size() == 1){
			cl = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::checkCompanyRepeat::too many company");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_COMAPNY.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		if(cl == null){ 
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{ 
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_COMAPNY.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	
	/**
	 * 企业注册
	 */
	@Transactional
	public ReturnPojo registerCompany(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String companyName = jb.getString("companyName");
		String adminPhone = jb.getString("adminPhone");
		String adminPwd = jb.getString("adminPwd");
		String companyType = jb.getString("companyType");
		String atUserId = jb.getString("atUserId");
		String editUserId = args.getEditUserId();
		String editTime = args.getEditTime();
		if(StringUtils.isEmpty(companyName) ||  StringUtils.isEmpty(adminPhone)
				|| StringUtils.isEmpty(adminPwd)|| StringUtils.isEmpty(companyType)|| StringUtils.isEmpty(atUserId)){
			logger.info("nsy-auth::registerCompany:: missing args: [companyName,adminPhone,adminPwd,companyType,atUserId]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}

		String currTime = Utils.getCurrTimeMillions();
		CompanyList cl = new CompanyList();
		cl.setCompanyId(atUserId);
		cl = companyDao.getCompanyById(cl);
		
		//判断注册手机号是否重复，公司名是否重复
		if(null == cl){ //手机号可注册
			
			CompanyList cpny = new CompanyList();
			cpny.setCompanyName(companyName);
			List<CompanyList> cplist = companyDao.getCompanyByCondition(cpny);
			if(null == cplist || cplist.size() == 0){
				cpny = null;
			}else if(cplist.size() == 1){
				cpny = cplist.get(0);
			}else if(cplist.size() > 1){
				logger.error("nsy-auth::registerCompany::too many company");
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_COMAPNY.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
			
			if(null == cpny){ //公司名可注册
				UserList ul = new UserList();
				ul.setAtUserId(atUserId);
				ul = userDao.getUserById(ul);
				String companyId = atUserId;
				CompanyList company = new CompanyList();
				company.setCompanyId(companyId);
				company.setCompanyName(companyName);
				company.setCompanyType(companyType);
				company.setFounderId(atUserId);
				company.setFounderName(ul.getRealName());
				company.setAdminId(atUserId);
				company.setAdminName(ul.getRealName());
				company.setEditTime(editTime);
				company.setEditUserId(editUserId);
				company.setSyncTime(currTime);
				company.setSyncType("0");
				company.setCreateTime(currTime);
				company.setIndividual("0");
				company.setValidity("1");
				companyDao.insertCompany(company);
				logger.info("nsy-user::registerCompany add company end ...");
				//初始化prop
				CompanyProp prop = new CompanyProp();
				prop.setCompanyId(companyId);
				prop.setPropName("adminPwd");
				prop.setPropValue(adminPwd);
				prop.setEditUserId(editUserId);
				prop.setEditTime(editTime);
				prop.setSyncTime(currTime);
				prop.setSyncType("0");
				prop.setCreateTime(currTime);
				List<CompanyProp> props = new ArrayList<CompanyProp>();
				props.add(prop);
				companyPropDao.insertBatchCompanyProp(props);
				logger.info("nsy-user::registerCompany add company prop end ...");
				//初始化工商信息
				BusinessLicense blice = new BusinessLicense();
				blice.setCompanyId(companyId);
				blice.setValidity("1");
				blice.setEditTime(editTime);
				blice.setEditUserId(editUserId);
				blice.setCreateTime(currTime);
				blice.setSyncTime(currTime);
				blice.setSyncType("0");
				businessLicenseDao.insertBusinessLicense(blice);
				logger.info("nsy-user::registerCompany add businessLicense end ...");
				String departmentId = "";
				String departmentName = "";
				if("100".equals(companyType)) {
					CompanyDepartment dept = new CompanyDepartment();
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setType("1");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setFatherId(companyId);
					dept.setFatherName(companyName);
					dept.setSonDepart("经营部");
					dept.setLevelNum("1");
					dept.setValidity("1");
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("销售部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("采购部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("财务部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("库房管理部");
					departmentDao.insertDepartment(dept);
					
					String defultId = Utils.makeDepartmentId(); //默认部门ID
					dept.setDepartmentId(defultId);
					dept.setSonDepart("农商网络管理部");
					departmentDao.insertDepartment(dept);
					
					departmentId = Utils.makeDepartmentId();
					departmentName = "农商网络管理部";
					dept.setDepartmentId(departmentId);
					dept.setType("2");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setFatherId(defultId);
					dept.setFatherName("农商网络管理部");
					dept.setSonDepart("网络管理员");
					dept.setLevelNum("30");
					dept.setValidity("1");
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
				} else if("200".equals(companyType)){
					CompanyDepartment dept = new CompanyDepartment();
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setType("1");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setFatherId(companyId);
					dept.setFatherName(companyName);
					dept.setSonDepart("财务部");
					dept.setLevelNum("1");
					dept.setValidity("1");
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("收银部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("采购部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("厨房管理部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("前厅管理部");
					departmentDao.insertDepartment(dept);
					
					dept.setDepartmentId(Utils.makeDepartmentId());
					dept.setSonDepart("库房管理部");
					departmentDao.insertDepartment(dept);
					
					String defultId = Utils.makeDepartmentId(); //默认部门ID
					dept.setDepartmentId(defultId);
					dept.setSonDepart("农商网络管理部");
					departmentDao.insertDepartment(dept);
					
					departmentId = Utils.makeDepartmentId();
					departmentName = "农商网络管理部";
					dept.setDepartmentId(departmentId);
					dept.setType("2");
					dept.setCompanyId(companyId);
					dept.setCompanyName(companyName);
					dept.setFatherId(defultId);
					dept.setFatherName("农商网络管理部");
					dept.setSonDepart("网络管理员");
					dept.setLevelNum("30");
					dept.setValidity("1");
					dept.setEditUserId(editUserId);
					dept.setEditTime(editTime);
					dept.setSyncTime(currTime);
					dept.setSyncType("0");
					dept.setCreateTime(currTime);
					departmentDao.insertDepartment(dept);
				}else{
					throw new ApplicationException("company type error " + companyType);
				}
				logger.info("nsy-user::registerCompany add department end ...");
				//公司关联员工
				EmployeeInfo emp = new EmployeeInfo();
				emp.setAtUserId(atUserId);
				emp.setRealName(ul.getRealName());
				emp.setCompanyId(companyId);
				emp.setCompanyName(companyName);
				emp.setDepartmentId(departmentId);
				emp.setDepartmentName(departmentName);
				emp.setPosition("网络管理员");
				emp.setValidity("1");
				emp.setOpAuthority("1");//管理员
				emp.setCreater("1");
				emp.setAuditIsPassed("1");
				emp.setEditTime(editTime);
				emp.setEditTime(editTime);
				emp.setSyncTime(currTime);
				emp.setSyncType("0");
				emp.setCreateTime(currTime);
				employeeInfoDao.insertEmployeeInfo(emp);
				logger.info("nsy-user::registerCompany add employee end ...");
				rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
				rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
				CompanyList recompany = new CompanyList();
				recompany.setCompanyId(company.getCompanyId());
				rp.setResult(recompany);
				
				//初始化商品
				List<BuyGoods> bgs = new ArrayList<BuyGoods>();
				Goods g = new Goods();
				if("100".equals(companyType)){
					g.setRawSelect("1");
					List<Goods> list = goodsDao.getGoodsByCondition(g);
					for(int i = 0, size = list.size(); i < size; i++){
						g = list.get(i);
						BuyGoods bg = new BuyGoods();
						bg.setBuyGoodsId(Utils.makeProductId());
						bg.setCompanyId(companyId);
						bg.setCompanyName(companyName);
						bg.setCompanyType(companyType);
						bg.setOpAuthority(g.getOpAuthority());
						bg.setValidity(g.getValidity());
						bg.setCommodity(g.getCommodity());
						bg.setFather(g.getFather());
						bg.setSon(g.getSon());
						bg.setLevelNum(g.getLevelNum());
						bg.setProvince(g.getProvince());
						bg.setCity(g.getCity());
						bg.setCompanyDefaultState(g.getCompanyDefaultState());
						bg.setProQuantity(g.getProQuantity());
						bg.setGoodsKingdom(g.getGoodsKingdom());
						bg.setUnit(g.getUnit());
						bg.setSelected("0");
						bg.setSequenceNum(g.getSequenceNum());
						bg.setEditUserId(editUserId);
						bg.setEditTime(editTime);
						bg.setSyncType("0");
						bg.setSyncTime(currTime);
						bg.setCreateTime(currTime);
						bgs.add(bg);
					}
				}else if("200".equals(companyType)){
					g.setSaleSelect("1");
					List<Goods> list = goodsDao.getGoodsByCondition(g);
					for(int i = 0, size = list.size(); i < size; i++){
						g = list.get(i);
						BuyGoods bg = new BuyGoods();
						bg.setBuyGoodsId(Utils.makeProductId());
						bg.setCompanyId(companyId);
						bg.setCompanyName(companyName);
						bg.setCompanyType(companyType);
						bg.setOpAuthority(g.getOpAuthority());
						bg.setValidity(g.getValidity());
						bg.setCommodity(g.getCommodity());
						bg.setFather(g.getFather());
						bg.setSon(g.getSon());
						bg.setLevelNum(g.getLevelNum());
						bg.setProvince(g.getProvince());
						bg.setCity(g.getCity());
						bg.setCompanyDefaultState(g.getCompanyDefaultState());
						bg.setProQuantity(g.getProQuantity());
						bg.setGoodsKingdom(g.getGoodsKingdom());
						bg.setUnit(g.getUnit());
						bg.setSelected("0");
						bg.setSequenceNum(g.getSequenceNum());
						bg.setEditUserId(editUserId);
						bg.setEditTime(editTime);
						bg.setSyncType("0");
						bg.setSyncTime(currTime);
						bg.setCreateTime(currTime);
						bgs.add(bg);
					}
				}
				buyGoodsDao.insertBatchBuyGoods(bgs);
				logger.info("nsy-user::register init goods end ...");
			}else{
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.HAVE_COMAPNY.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
			}
			
		}else{
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OTHER_COMPANY_REGISTER.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 登录
	 */
	@Transactional
	public ReturnPojo login(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String currTime = Utils.getCurrTime();
		String currMillTime = Utils.getCurrTimeMillions();
		String business = args.getBusiness();
		logger.info("nsy-auth::login::params::" + business);
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		String registerNum = jb.getString("registerNum");
		String registerType = jb.getString("registerType");
		String pwd = jb.getString("pwd");
		String isWX = jb.getString("isWX");
		String openId = jb.getString("openId");
		if(StringUtils.isEmpty(registerNum) || StringUtils.isEmpty(registerType) || StringUtils.isEmpty(pwd)
				){
			logger.info("nsy-auth::login:: missing args: [registerNum,registerType,pwd]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		boolean isPhone = false;
		UserList ul = new UserList();
		if("p".equals(registerType.toLowerCase())){
			ul.setAtUserId(registerNum);
			isPhone = true;
		}else if("q".equals(registerType.toLowerCase())){
			ul.setQqNum(registerNum);
		}else if("w".equals(registerType.toLowerCase())){
			ul.setWeChatNum(registerNum);
		}else if("e".equals(registerType.toLowerCase())){
			ul.setEmailAddr(registerNum);
		}
		UserList ua = null;
		if(isPhone){
			logger.info("nsy-auth::login::is phone");
			ua = userDao.getUserById(ul);
		}else{
			logger.info("nsy-auth::login::not phone");
			ua = new UserList();
			ua.setPhoneNum(registerNum);
			List<UserList> list = userDao.getUserByCondition(ua);
			if(null == list || list.size() == 0){
				ua = null;
			}else if(list.size() == 1){
				ua = list.get(0);
			}else if(list.size() > 1){
				logger.error("nsy-auth::login::too many user");
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOO_MANY_DATA.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
		}
		 
		if(null == ua){ //没有账号
			logger.info("nsy-auth::register no exists");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.NOT_HAVE_ACCOUNT.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		
		UserPassword uw = new UserPassword();
		uw.setAtUserId(registerNum);
		List<UserPassword> list = userPasswordDao.getUserPasswordByCondition(uw);
		if(null == list || list.size() == 0){
			uw = null;
		}else if(list.size() == 1){
			uw = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::login::too many user password");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.LOGIN_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		
		boolean pwdFlag = false;
		if(null != uw){
			if(pwd.equals(uw.getUserPwd())){
				pwdFlag = true; //密码匹配成功
			}
		}
		
		if(pwdFlag){
			logger.info("nsy-auth::login::password OK");
			String makeToken;
			try {
				makeToken = TokenUtils.getToken();
			} catch (NoSuchAlgorithmException e1) {
				logger.error("nsy-auth::login::token make fail" + e1.getMessage());
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOKEN_MAKE_FAIL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
			logger.info("nsy-auth::login::login in, make token insert log");
			if(!StringUtils.isEmpty(isWX) && "yes".equals(isWX)){ //如果是微信则登录绑定
				if(!StringUtils.isEmpty(openId) && !"null".equals(openId)){
					logger.info("nsy-auth::login::binding openId" + openId);
					UserPassword pass = new UserPassword();
					pass.setPasswordId(uw.getPasswordId());
					pass.setOpenId(openId);
					pass.setEditTime(args.getEditTime());
					pass.setEditUserId(args.getEditUserId());
					pass.setSyncTime(currMillTime);
					userPasswordDao.updateUserPassword(pass);
				}
			}
			logger.info("nsy-auth::login::start insert login log");
			LoginLog loginLog = new LoginLog();
			loginLog.setLogType("1");
			loginLog.setLogContent("登录");
			loginLog.setLogTime(currTime);
			loginLog.setLogDevice(args.getDevice());
			loginLog.setLogIp(args.getIpaddr());
			loginLog.setLogMac(args.getMac());
			loginLog.setToken(makeToken);
			loginLog.setAtUserId(registerNum);
			loginLog.setEditTime(args.getEditTime());
			loginLog.setEditUserId(args.getEditUserId());
			loginLog.setSyncTime(currMillTime);
			loginLog.setSyncType("0");
			loginLog.setCreateTime(currMillTime);
			loginLogDao.insertLoginLog(loginLog);
			JSONObject result = new JSONObject();
			result.put("token", makeToken);
			result.put("userList", ua);
			result.put("atUserId", registerNum);
			if(!StringUtils.isEmpty(isWX) && "yes".equals(isWX)){
				result.put("openId", uw.getOpenId());
			}
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
			rp.setResult(result);
		}else{
			logger.info("nsy-auth::login::password not OK");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.PASSWORD_ERROR.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 自动登录微信
	 */
	@Transactional
	public ReturnPojo autoLoginWX(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String currTime = Utils.getCurrTime();
		String currMillTime = Utils.getCurrTimeMillions();
		String business = args.getBusiness();
		logger.info("nsy-auth::autoLoginWX::business::" + business);
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		String openId = jb.getString("openId");
		logger.info("nsy-auth::autoLoginWX::openId::" + openId);
		if(StringUtils.isEmpty(openId)){
			logger.info("nsy-auth::autoLoginWX:: missing args: [openId]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		
		UserPassword uw = new UserPassword();
		uw.setOpenId(openId);
		List<UserPassword> list = userPasswordDao.getUserPasswordByCondition(uw);
		if(null == list || list.size() == 0){
			logger.info("nsy-auth::autoLoginWX::openId not bind user");
			uw = null;
		}else if(list.size() == 1){
			logger.info("nsy-auth::autoLoginWX::openId bind user");
			uw = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::autoLoginWX::too many openId");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOO_MANY_DATA.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		if(null != uw){ 
			String makeToken;
			try {
				makeToken = TokenUtils.getToken();
				logger.error("nsy-auth::autoLoginWX::token making");
			} catch (NoSuchAlgorithmException e1) {
				logger.error("nsy-auth::autoLoginWX::token make fail" + e1.getMessage());
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOKEN_MAKE_FAIL.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
			logger.info("nsy-auth::autoLoginWX::openId login OK, start loginLog::");
			
			LoginLog loginLog = new LoginLog();
			loginLog.setLogType("1");
			loginLog.setLogContent("登录");
			loginLog.setLogTime(currTime);
			loginLog.setLogDevice(args.getDevice());
			loginLog.setLogIp(args.getIpaddr());
			loginLog.setLogMac(args.getMac());
			loginLog.setToken(makeToken);
			loginLog.setAtUserId(uw.getAtUserId());
			loginLog.setEditTime(args.getEditTime());
			loginLog.setEditUserId(args.getEditUserId());
			loginLog.setSyncTime(currMillTime);
			loginLog.setSyncType("0");
			loginLog.setCreateTime(currMillTime);
			loginLogDao.insertLoginLog(loginLog);
			//登录成功后，返回用户验证信息
			JSONObject result = new JSONObject();
			result.put("token", makeToken);
			result.put("openId", openId);
			result.put("atUserId", uw.getAtUserId());
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
			rp.setResult(result);
		}else{
			logger.info("nsy-auth::autoLoginWX::openId not binding::");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OPENID_NOT_BINDING.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 检查openId绑定WX
	 */
	@Transactional
	public ReturnPojo checkOpenIdBinding(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String openId = jb.getString("openId");
		String registerNum = jb.getString("registerNum");
		if(StringUtils.isEmpty(openId) || StringUtils.isEmpty(registerNum)){
			logger.info("nsy-auth::checkOpenIdBinding:: missing args: [openId,registerNum]");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		logger.info("nsy-auth::checkOpenIdBinding:: valid openId exists");
		UserPassword uw = new UserPassword();
		uw.setOpenId(openId);
		List<UserPassword> list = userPasswordDao.getUserPasswordByCondition(uw);
		if(null == list || list.size() == 0){
			uw = null;
		}else if(list.size() == 1){
			uw = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::checkOpenIdBinding::too many openId");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOO_MANY_DATA.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		if(null == uw){//openId没有绑定任何账号，判断即将绑定的账号是否已经绑定微信号
			logger.info("nsy-auth::checkOpenIdBinding:: openId not binding");
			uw = new UserPassword();
			uw.setAtUserId(registerNum);
			List<UserPassword> ulist = userPasswordDao.getUserPasswordByCondition(uw);
			if(null == ulist || ulist.size() == 0){
				uw = null;
			}else if(ulist.size() == 1){
				uw = ulist.get(0);
			}else if(ulist.size() > 1){
				logger.error("nsy-auth::checkOpenIdBinding::too many atUserId");
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOO_MANY_DATA.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
				return rp;
			}
			if(null == uw || StringUtils.isEmpty(uw.getOpenId())){ //账号没有绑定任何openId
				logger.info("nsy-auth::checkOpenIdBinding:: registerNum no openId ");
				rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
				rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
			}else{
				logger.info("nsy-auth::checkOpenIdBinding:: registerNum have openId ");
				ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OTHER_OPENID_BINDING.getCode());
				rp.setReturnCode(rc.getCode());
				rp.setReturnMsg(rc.getMessage());
			}
		}else{
			logger.info("nsy-auth::checkOpenIdBinding:: openId binding");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.OPENID_HAVE_BINDING.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}
		return rp;
	}
	
	/**
	 * 查询用户信息
	 */
	public ReturnPojo queryUserInfo(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		logger.info("nsy-auth::queryUserInfo::params::" + business);
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		if(StringUtils.isEmpty(atUserId) ){
			logger.info("nsy-auth::login:: missing args: [atUserId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		//获取账号信息
		JSONObject json = new JSONObject();
		UserList ac = new UserList();
		ac.setAtUserId(atUserId);
		ac = userDao.getUserById(ac);
		if(null != ac){
			logger.info("nsy-auth::queryUserInfo::user OK::");
			json.put("atUserId", ac.getAtUserId());
			json.put("nickName", ac.getNickName());
			json.put("sex", ac.getSex());
			json.put("realName", ac.getRealName());
		}
		//获取部门信息
		EmployeeInfo emp = new EmployeeInfo();
		emp.setAtUserId(atUserId);
		emp.setAuditIsPassed("1"); //已经是公司人员
		List<EmployeeInfo> list = employeeInfoDao.getEmployeeInfoByCondition(emp);
		if(null == list || list.size() == 0){
			emp = null;
		}else if(list.size() == 1){
			emp = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::queryUserInfo::too many employee");
			rp.setReturnCode(ReturnCodes.TOO_MANY_DATA.getCode());
			rp.setReturnMsg(ReturnCodes.TOO_MANY_DATA.getMessage());
			return rp;
		}
		if(null != emp){ //查询所属公司信息
			logger.error("nsy-auth::queryUserInfo::company info OK");
			CompanyList cl = new CompanyList();
			cl.setCompanyId(emp.getCompanyId());
			cl = companyDao.getCompanyById(cl);
			if(null != cl){
				logger.error("nsy-auth::queryUserInfo::get company info");
				json.put("companyId", cl.getCompanyId());
				json.put("companyName", cl.getCompanyName());
				json.put("companyNickName", cl.getCompanyNickName());
				json.put("companyProvince", cl.getProvince());
				json.put("companyCity", cl.getCity());
				json.put("companyDistrict", cl.getDistrict());
				json.put("companyMarket", cl.getMarket());
				json.put("companyAuthority", emp.getOpAuthority());
				//prop信息
				CompanyProp cp = new CompanyProp();
				cp.setCompanyId(cl.getCompanyId());
				List<CompanyProp> cprops = companyPropDao.getCompanyPropByCondition(cp);
				logger.error("nsy-auth::queryUserInfo::get company prop info");
				if(null != cprops && cprops.size() > 0){
					for(int i = 0, size = cprops.size(); i < size; i++ ){
						cp = cprops.get(i);
						json.put(cp.getPropName(), cp.getPropValue());
					}
				}
				//工商信息
				BusinessLicense blice = new BusinessLicense();
				blice.setCompanyId(cl.getCompanyId());
				List<BusinessLicense> blist = businessLicenseDao.getBusinessLicenseByCondition(blice);
				logger.error("nsy-auth::queryUserInfo::get businessLicense info");
				if(null == blist || blist.size() == 0){
					blice = null;
				}else if(blist.size() == 1){
					blice = blist.get(0);
				}else if(blist.size() > 1){
					logger.error("nsy-auth::queryUserInfo::too many businessLicense");
					rp.setReturnCode(ReturnCodes.TOO_MANY_DATA.getCode());
					rp.setReturnMsg(ReturnCodes.TOO_MANY_DATA.getMessage());
					return rp;
				}
				json.put("businessRegisterCode", blice.getBusinessRegisterCode());
				json.put("legalRepresentative", blice.getLegalRepresentative());
				json.put("termStart", blice.getTermStart());
				json.put("termEnd", blice.getTermEnd());
				json.put("industry", blice.getIndustry());
				json.put("viceIndustry", blice.getViceIndustry());
				json.put("introduc", blice.getIntroduc());
			}
			json.put("departmentId", emp.getDepartmentId());
			json.put("departmentName", emp.getDepartmentName());
		}
		UserProp up = new UserProp();
		up.setAtUserId(atUserId);
		List<UserProp> props = userPropDao.getUserPropByCondition(up);
		logger.error("nsy-auth::queryUserInfo::get user prop info");
		if(null != props && props.size() > 0){
			for(int i = 0, size = props.size(); i < size; i++ ){
				up = props.get(i);
				json.put(up.getPropName(), up.getPropValue());
			}
		}
		rp.setResult(json);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 查询公司信息
	 */
	public ReturnPojo queryCompanyInfo(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		if(StringUtils.isEmpty(atUserId) ){
			logger.info("nsy-auth::login:: missing args: [atUserId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		JSONObject json = new JSONObject();
		if(StringUtils.isEmpty(companyId)){ //为空，使用atUserId查信息
			EmployeeInfo emp = new EmployeeInfo();
			emp.setAtUserId(atUserId);
			emp.setAuditIsPassed("1");
			List<EmployeeInfo> list = employeeInfoDao.getEmployeeInfoByCondition(emp);
			if(null == list || list.size() == 0){
				emp = null;
			}else if(list.size() == 1){
				emp = list.get(0);
			}else if(list.size() > 1){
				logger.error("nsy-auth::queryCompanyInfo::too many user");
				rp.setReturnCode(ReturnCodes.QUERY_COMPANY_INFO_FAIL.getCode());
				rp.setReturnMsg(ReturnCodes.QUERY_COMPANY_INFO_FAIL.getMessage());
				return rp;
			}
			if(null == emp){
				logger.error("nsy-auth::queryCompanyInfo::user no have company");
				rp.setReturnCode(ReturnCodes.NOT_HAVE_COMAPNY.getCode());
				rp.setReturnMsg(ReturnCodes.NOT_HAVE_COMAPNY.getMessage());
				return rp;
			}else{
				companyId = emp.getCompanyId();
			}
		}
		CompanyList company = new CompanyList();
		company.setCompanyId(companyId);
		company = companyDao.getCompanyById(company);
		json.put("companyId", company.getCompanyId());
		json.put("companyName", company.getCompanyName());
		json.put("companyType", company.getCompanyType());
		json.put("province", company.getProvince());
		json.put("city", company.getCity());
		json.put("district", company.getDistrict());
		CompanyProp up = new CompanyProp();
		up.setCompanyId(companyId);
		List<CompanyProp> props = companyPropDao.getCompanyPropByCondition(up);
		if(null != props && props.size() > 0){
			for(int i = 0, size = props.size(); i < size; i++ ){
				up = props.get(i);
				if("adminPwd".equals(up)){
					continue;
				}
				json.put(up.getPropName(), up.getPropValue());
			}
		}
		BusinessLicense bl = new BusinessLicense();
		bl.setCompanyId(companyId);
		bl = businessLicenseDao.getBusinessLicenseById(bl);
		if(null != bl){
			json.put("industry", bl.getIndustry());
			json.put("viceIndustry", bl.getViceIndustry());
			json.put("scope", bl.getScope());
			json.put("businessRegisterCode", bl.getBusinessRegisterCode());
			json.put("legalRepresentative", bl.getLegalRepresentative());
			json.put("termStart", bl.getTermStart());
			json.put("termEnd", bl.getTermEnd());
		}
		rp.setResult(json);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 修改公司信息
	 */
	@Transactional
	public ReturnPojo resetCompanyInfo(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String companyName = jb.getString("companyName");
		String thumbnail = jb.getString("thumbnail");
		String companyType = jb.getString("companyType");
		String province = jb.getString("province");
		String city = jb.getString("city");
		String district = jb.getString("district");
		String market = jb.getString("market");
		String industry = jb.getString("industry");
		String viceIndustry = jb.getString("viceIndustry");
		String scope = jb.getString("scope");
		String postalAddress =jb.getString("postalAddress");
		String website = jb.getString("website");
		String telephone = jb.getString("telephone");
		String mailbox = jb.getString("mailbox");
		String businessRegisterCode = jb.getString("businessRegisterCode");
		String legalRepresentative =  jb.getString("legalRepresentative");
		String termStart = jb.getString("termStart");
		String termEnd = jb.getString("termEnd");
		String editUserId = args.getEditUserId();
		String editTime = args.getEditTime();
		String currTime = Utils.getCurrTimeMillions();
		jb.put("editUserId", editUserId);
		jb.put("editTime", editTime);
		jb.put("syncTime", currTime);
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) || (null == companyName && null == thumbnail 
				&& null == companyType&& null == province && null == city && null == district && null == market && null == industry 
				&& null == viceIndustry && null == scope && null == postalAddress && null == website && null == telephone 
				&& null == mailbox && null == businessRegisterCode && null == legalRepresentative && null == termStart 
				&& null == termEnd)){
			logger.info("nsy-auth::login:: missing args: [atUserId,companyId] and [companyName] or [thumbnail] or [companyType] or "
					+ "[province] or [city] or [district] or [market] or [industry] or [viceIndustry] or [scope] or [postalAddress] or "
					+ "[website] or [telephone]  or [mailbox]  or [businessRegisterCode]  or [legalRepresentative]  or [termStart] or [termStart] ");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		//如果修改的是company_list表中的数据则修改。
		if(StringUtils.isEmpty(companyName) || StringUtils.isEmpty(thumbnail) || StringUtils.isEmpty(companyType) || 
				StringUtils.isEmpty(province) || StringUtils.isEmpty(city) || StringUtils.isEmpty(district) ||
				StringUtils.isEmpty(market) ){
			
		}
		CompanyList company = JSON.toJavaObject(jb, CompanyList.class);
		companyDao.updateCompany(company);
		logger.info("nsy-auth::resetCompanyInfo:: update company end");
		boolean isTb = (null != thumbnail);
		if(isTb){
			UserProp tb = null;
			UserProp rc = null;
			UserProp up = new UserProp();
			up.setAtUserId(atUserId);
			List<UserProp> props = userPropDao.getUserPropByCondition(up);
			for(int i = 0; i < props.size(); i++){
				up = props.get(i);
				//选择了并且名字一致
				if(isTb && "thumbnail".equals(up.getPropName())){
					tb = up;
				}
			}
			//被选择并且存在prop里
			if(isTb && null != tb){
				tb.setPropValue(thumbnail);
				userPropDao.updateUserProp(tb);
				logger.info("nsy-auth::resetUserInfo:: thumbnail update " + thumbnail);
			}else if(isTb && null == tb){//被选择但不存在prop里
				tb = new UserProp();
				tb.setPropName("thumbnail");
				tb.setPropValue(thumbnail);
				tb.setAtUserId(atUserId);
				tb.setEditUserId(editUserId);
				tb.setEditTime(editTime);
				tb.setSyncTime(currTime);
				tb.setSyncType("0");
				tb.setCreateTime(currTime);
				userPropDao.insertUserProp(tb);
				logger.info("nsy-auth::resetUserInfo:: thumbnail insert " + thumbnail);
			}
		}
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 添加收发货地址
	 */
	@Transactional
	public ReturnPojo addDeliverAddress(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String province = jb.getString("province");
		String city = jb.getString("city");
		String district = jb.getString("district");
		String market = jb.getString("market");
		String receiveUserName = jb.getString("receiveUserName");
		String receivePhone = jb.getString("receivePhone");
		String sendUserName = jb.getString("sendUserName");
		String sendPhone =jb.getString("sendPhone");
		String zipcode = jb.getString("zipcode");
		String detailAddress = jb.getString("detailAddress");
		String isDefault = jb.getString("isDefault");
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) || StringUtils.isEmpty(province)
				|| StringUtils.isEmpty(city) || StringUtils.isEmpty(district) || StringUtils.isEmpty(receiveUserName)
				|| StringUtils.isEmpty(receivePhone) || StringUtils.isEmpty(sendUserName) || StringUtils.isEmpty(sendPhone)
				|| StringUtils.isEmpty(zipcode) || StringUtils.isEmpty(detailAddress) || StringUtils.isEmpty(isDefault)){
			logger.info("nsy-auth::login:: missing args: [atUserId,companyId,province,city,district,receiveUserName,receivePhone,sendUserName,sendPhone,zipcode,detailAddress,isDefault]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		DeliverAddress address = new DeliverAddress();
		address.setCompanyId(companyId);
		address.setProvince(province);
		address.setCity(city);
		address.setDistrict(district);
		address.setMarket(market);
		address.setReceiveUserName(receiveUserName);
		address.setReceivePhone(receivePhone);
		address.setSendUserName(sendUserName);
		address.setSendPhone(sendPhone);
		address.setZipcode(zipcode);
		address.setDetailAddress(detailAddress);
		address.setIsDefault(isDefault);
		address.setEditTime(args.getEditTime());
		address.setEditUserId(args.getEditUserId());
		address.setValidity("1");
		address.setSyncTime(currtime);
		address.setSyncType("0");
		address.setCreateTime(currtime);
		deliverAddressDao.insertDeliverAddress(address); 
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 修改收发货地址
	 */
	@Transactional
	public ReturnPojo updateDeliverAddress(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String deliverAddressId = jb.getString("deliverAddressId");
		String province = jb.getString("province");
		String city = jb.getString("city");
		String district = jb.getString("district");
		String market = jb.getString("market");
		String receiveUserName = jb.getString("receiveUserName");
		String receivePhone = jb.getString("receivePhone");
		String sendUserName = jb.getString("sendUserName");
		String sendPhone =jb.getString("sendPhone");
		String zipcode = jb.getString("zipcode");
		String detailAddress = jb.getString("detailAddress");
		String isDefault = jb.getString("isDefault");
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) || StringUtils.isEmpty(deliverAddressId) 
				|| ((null == province && null == city 
						&& null == district&& null == market && null == receiveUserName && null == receivePhone 
						&& null == sendUserName && null == sendPhone 
						&& null == zipcode && null == detailAddress && null == isDefault ))){
			logger.info("nsy-auth::login:: missing args: [atUserId,companyId,deliverAddressId] and province,city,district,receiveUserName,receivePhone,sendUserName,sendPhone,zipcode,detailAddress,isDefault");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		if(!StringUtils.isEmpty(isDefault)){ //修改默认地址前重置所有地址为不默认
			DeliverAddress clearAddress = new DeliverAddress();
			clearAddress.setCompanyId(companyId);
			deliverAddressDao.clearDeliverAddressDefault(clearAddress);
		}
		
		DeliverAddress address = new DeliverAddress();
		address.setDeliverAddressId(deliverAddressId);
		address.setCompanyId(companyId);
		address.setProvince(province);
		address.setCity(city);
		address.setDistrict(district);
		address.setMarket(market);
		address.setReceiveUserName(receiveUserName);
		address.setReceivePhone(receivePhone);
		address.setSendUserName(sendUserName);
		address.setSendPhone(sendPhone);
		address.setZipcode(zipcode);
		address.setDetailAddress(detailAddress);
		address.setIsDefault(isDefault);
		address.setEditTime(args.getEditTime());
		address.setEditUserId(args.getEditUserId());
		address.setSyncTime(currtime);
		deliverAddressDao.updateDeliverAddress(address); 
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 登出
	 */
	@Transactional
	public ReturnPojo logout(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String currTime = Utils.getCurrTime();
		String currMillTime = Utils.getCurrTimeMillions();
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		if(StringUtils.isEmpty(atUserId) ){
			logger.info("nsy-auth::login:: missing args: [registerNum,registerType,pwd]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		LoginLog loginLog = new LoginLog();
		loginLog.setLogType("2");
		loginLog.setLogContent("注销");
		loginLog.setLogTime(currTime);
		loginLog.setLogDevice(args.getDevice());
		loginLog.setLogIp(args.getIpaddr());
		loginLog.setLogMac(args.getMac());
		loginLog.setToken("");
		loginLog.setAtUserId(atUserId);
		loginLog.setEditTime(args.getEditTime());
		loginLog.setEditUserId(args.getEditUserId());
		loginLog.setSyncTime(currMillTime);
		loginLog.setSyncType("0");
		loginLog.setCreateTime(currMillTime);
		loginLogDao.insertLoginLog(loginLog);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 修改个人信息
	 */
	@Transactional
	public ReturnPojo resetUserInfo(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String nickName = jb.getString("nickName");
		String thumbnail = jb.getString("thumbnail");
		String sex = jb.getString("sex");
		String realName = jb.getString("realName");
		String rqCode = jb.getString("rqCode");
		String editUserId = args.getEditUserId();
		String editTime = args.getEditTime();
		String currTime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || (null == nickName && null == thumbnail &&
				null == sex&& null == realName && null == rqCode) ){
			logger.info("nsy-auth::resetUserInfo:: missing args: [atUserId] and [nickName] or [pic] or [sex] or [realName] or [rqCode]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		
		UserList ul = new UserList();
		ul.setAtUserId(atUserId);
		ul = userDao.getUserById(ul);
		if(null != nickName){
			ul.setNickName(nickName);
		}
		if(null != sex){
			ul.setSex(sex);
		}
		if(null != realName){
			ul.setRealName(realName);
		}
		ul.setEditUserId(editUserId);
		ul.setEditTime(editTime);
		ul.setSyncTime(currTime);
		userDao.updateUser(ul);
		logger.info("nsy-auth::resetUserInfo:: update user end");
		//存在为true
		boolean isTb = (null != thumbnail);
		boolean isRC = (null != rqCode);
		if(isTb || isRC){
			UserProp tb = null;
			UserProp rc = null;
			UserProp up = new UserProp();
			up.setAtUserId(atUserId);
			List<UserProp> props = userPropDao.getUserPropByCondition(up);
			for(int i = 0; i < props.size(); i++){
				up = props.get(i);
				//选择了并且名字一致
				if(isTb && "thumbnail".equals(up.getPropName())){
					tb = up;
				}
				if(isRC && "rqCode".equals(up.getPropName())){
					rc = up;
				}
			}
			//被选择并且存在prop里
			if(isTb && null != tb){
				tb.setPropValue(thumbnail);
				userPropDao.updateUserProp(tb);
				logger.info("nsy-auth::resetUserInfo:: thumbnail update " + thumbnail);
			}else if(isTb && null == tb){//被选择但不存在prop里
				tb = new UserProp();
				tb.setPropName("thumbnail");
				tb.setPropValue(thumbnail);
				tb.setAtUserId(atUserId);
				tb.setEditUserId(editUserId);
				tb.setEditTime(editTime);
				tb.setSyncTime(currTime);
				tb.setSyncType("0");
				tb.setCreateTime(currTime);
				userPropDao.insertUserProp(tb);
				logger.info("nsy-auth::resetUserInfo:: thumbnail insert " + thumbnail);
			}
			//被选择并且存在prop里
			if(isRC && null != rc){
				rc.setPropValue(rqCode);
				userPropDao.updateUserProp(rc);
				logger.info("nsy-auth::resetUserInfo:: rqCode update " + rqCode);
			}else if(isRC && null == rc){//被选择但不存在prop里
				rc = new UserProp();
				rc.setPropName("rqCode");
				rc.setPropValue(rqCode);
				rc.setAtUserId(atUserId);
				rc.setEditUserId(editUserId);
				rc.setEditTime(editTime);
				rc.setSyncTime(currTime);
				rc.setSyncType("0");
				rc.setCreateTime(currTime);
				userPropDao.insertUserProp(rc);
				logger.info("nsy-auth::resetUserInfo:: rqCode insert " + rqCode);
			}
		}
		logger.info("nsy-auth::resetUserInfo:: update user prop end");
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 查询公司渠道供应商
	 */
	public ReturnPojo queryCompanyRelation(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String goodsKingdom = jb.getString("goodsKingdom");
		String province = jb.getString("province");
		String companyId = jb.getString("companyId");
		String direction = jb.getString("direction");
		String count = jb.getString("count");
		String createTime = jb.getString("createTime");
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) 
				|| StringUtils.isEmpty(count) || StringUtils.isEmpty(direction)){
			logger.info("nsy-auth::queryCompanyRelation:: missing args: [atUserId],[companyId],[count],[direction]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		CompanyRelationship companyRelationship = new CompanyRelationship();
		companyRelationship.setCompanyId(companyId);
		companyRelationship.setCount(Integer.valueOf(count));
		companyRelationship.setDirection(Integer.valueOf(direction));
		Map<String, String> map = new HashMap<String, String>();
		map.put("companyId", companyId);
		map.put("count", count);
		map.put("direction", direction);
		map.put("goodsKingdom", goodsKingdom);
		map.put("province", province);
		map.put("createTime", createTime);
		List<CompanyRelationship> list = companyRelationshipDao.queryCompanyRelation(map);
		rp.setResult(list);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 查询某地区渠道供应商(添加渠道界面)
	 */
	public ReturnPojo queryAllRelation(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String province = jb.getString("province");
		String city = jb.getString("city");
		String direction = jb.getString("direction");
		String count = jb.getString("count");
		String createTime = jb.getString("createTime");
		if(StringUtils.isEmpty(direction)) {
			direction = "0";
		}
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(count) || StringUtils.isEmpty(direction)
				|| StringUtils.isEmpty(province) 
				|| StringUtils.isEmpty(companyId)){
			logger.info("nsy-auth::queryAllCompany:: missing args: [atUserId],[direction],[count],[province],[companyId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		CompanyList companyCity = new CompanyList();
		if(!StringUtils.isEmpty(createTime)) {
			companyCity.setCreateTime(createTime);
		} else {
			logger.info("nsy-auth:queryAllRelation create no have.");
		}
		companyCity.setProvince(province);
		companyCity.setCity(city);
		companyCity.setDirection(Integer.valueOf(direction));
		companyCity.setCount(Integer.valueOf(count));
		companyCity.setCompanyId(companyId);
		companyCity.setCompanyType("100");
		List<CompanyList> list = companyDao.getCompanyByNotSelf(companyCity);
		rp.setResult(list);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 添加渠道供应商
	 */
	@Transactional
	public ReturnPojo addCompanyRelation(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String channelCompanyId = jb.getString("channelCompanyId");
		String contactUserId = jb.getString("contactUserId");
		String goodsKingdom = jb.getString("goodsKingdom");
		if(StringUtils.isEmpty(atUserId)||StringUtils.isEmpty(companyId)||StringUtils.isEmpty(channelCompanyId)
				||StringUtils.isEmpty(contactUserId)
				||StringUtils.isEmpty(goodsKingdom)){
			logger.info("nsy-auth::addCompanyRelation:: missing args: [atUserId],[channelCompanyId],[contactUserId],[goodsKingdom]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		String currtime = Utils.getCurrTimeMillions();
		CompanyList company = new CompanyList();
		company.setCompanyId(companyId);
		company = companyDao.getCompanyById(company);
		
		CompanyList channel = new CompanyList();
		channel.setCompanyId(channelCompanyId);
		channel = companyDao.getCompanyById(channel);
		
		UserList contact = new UserList();
		contact.setAtUserId(contactUserId);
		contact = userDao.getUserById(contact);
		
		if(null == company || null == channel || null == contact){
			rp.setReturnCode(ReturnCodes.ADD_COMPANY_RELATION_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.ADD_COMPANY_RELATION_FAIL.getMessage());
			return rp;
		}
		CompanyRelationship cr = new CompanyRelationship();
		cr.setCompanyId(companyId);
		cr.setChannelCompanyId(channelCompanyId);;
		List<CompanyRelationship> list = companyRelationshipDao.getCompanyRelationshipByCondition(cr);
		if(null == list || list.size() == 0){
			cr = null;
		}else if(list.size() == 1){
			cr = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::addCompanyRelation::too many relationship");
			rp.setReturnCode(ReturnCodes.ADD_COMPANY_RELATION_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.ADD_COMPANY_RELATION_FAIL.getMessage());
			return rp;
		}
		
		if(cr == null){
			CompanyRelationship crs = new CompanyRelationship();
			crs.setChannelId(Utils.makeChannelId());
			crs.setCompanyId(companyId);
			crs.setCompanyName(company.getCompanyName());
			crs.setGoodsKingdom(goodsKingdom);
			crs.setChannelCompanyId(channelCompanyId);
			crs.setChannelCompanyName(channel.getCompanyName());
			crs.setContactUserId(contactUserId);
			crs.setContactName(contact.getRealName());
			crs.setContactPhone(contact.getPhoneNum());
			crs.setCreateTime(currtime);
			crs.setEditUserId(args.getEditUserId());
			crs.setEditTime(args.getEditTime());
			crs.setValidity("1");
			crs.setSyncType("0");
			crs.setSyncTime(currtime);
			companyRelationshipDao.insertCompanyRelationship(crs);
		}
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 获取所有公司员工
	 */
	public ReturnPojo getCompanyAllEmployee(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		if(StringUtils.isEmpty(atUserId)||StringUtils.isEmpty(companyId)){
			logger.info("nsy-auth::addCompanyRelation:: missing args: [atUserId],[companyId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		EmployeeInfo employeeInfo = new EmployeeInfo();
		employeeInfo.setCompanyId(companyId);
		List<EmployeeInfo> emps = employeeInfoDao.getEmployeeInfoByCondition(employeeInfo);
		JSONObject json = new JSONObject();
		JSONArray array = new JSONArray();
		for(int i = 0, size = emps.size(); i < size; i++){
			EmployeeInfo tmp = emps.get(i);
			JSONObject emp = new JSONObject();
			emp.put("departmentName", tmp.getDepartmentName());
			emp.put("realName", tmp.getRealName());
			emp.put("position", tmp.getPosition());
			emp.put("atUserId", tmp.getAtUserId());
			emp.put("employeeId", tmp.getEmployeeId());
			array.add(emp);
		}
		json.put("employees", array);
		rp.setResult(json);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 模糊查询公司
	 */
	public ReturnPojo queryVagueCompany(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyName = jb.getString("companyName");
		String count = jb.getString("count");
		if(StringUtils.isEmpty(atUserId)||StringUtils.isEmpty(companyName)
				||StringUtils.isEmpty(count)){
			logger.info("nsy-auth::queryVagueCompany:: missing args: [atUserId],[companyName],[count]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		CompanyList cps = new CompanyList();
		cps.setCompanyName(companyName);
		cps.setCount(Integer.parseInt(count));
		List<CompanyList> list = companyDao.queryVagueCompany(cps);
		JSONObject json = new JSONObject();
		json.put("companys", list);
		rp.setResult(json);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 公司存在
	 */
	public ReturnPojo existsCompany(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyName = jb.getString("companyName");
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyName)){
			logger.info("nsy-auth::existsCompany:: missing args: [atUserId, companyName]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		CompanyList cl = new CompanyList();
		cl.setCompanyName(companyName);
		List<CompanyList> list = companyDao.getCompanyByCondition(cl);
		if(null == list || list.size() == 0){
			cl = null;
		}else if(list.size() == 1){
			cl = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::existsCompany::too many company");
			rp.setReturnCode(ReturnCodes.HAVE_COMAPNY.getCode());
			rp.setReturnMsg(ReturnCodes.HAVE_COMAPNY.getMessage());
			return rp;
		}
		if(cl != null){ 
			JSONObject json = new JSONObject();
			json.put("companyId", cl.getCompanyId());
			rp.setResult(json);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{ 
			rp.setReturnCode(ReturnCodes.NOT_HAVE_COMAPNY.getCode());
			rp.setReturnMsg(ReturnCodes.NOT_HAVE_COMAPNY.getMessage());
		}
		return rp;
	}
	
	/**
	 * 加入公司
	 */
	@Transactional
	public ReturnPojo joinCompany(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String realName = jb.getString("realName");
		String companyId = jb.getString("companyId");
		String companyName = jb.getString("companyName");
		String departmentId = jb.getString("departmentId");
		String departmentName = jb.getString("departmentName");
		String position = jb.getString("position");
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(realName)
				|| StringUtils.isEmpty(companyId)|| StringUtils.isEmpty(companyName)
				|| StringUtils.isEmpty(departmentId)|| StringUtils.isEmpty(departmentName)
				){
			logger.info("nsy-auth::joinCompany:: missing args: [atUserId, companyName,companyId,realName,departmentId,departmentName]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		
		//判断是否已加入公司，有值说明已加入公司，同时判断是否已经申请过该公司
		EmployeeInfo loyee = new EmployeeInfo();
		loyee.setAtUserId(atUserId);
		loyee.setAuditIsPassed("1");
		List<EmployeeInfo> list = employeeInfoDao.getEmployeeInfoByCondition(loyee);
		if(null == list || list.size() == 0){
			loyee = null;
		}else if(list.size() == 1){
			loyee = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::joinCompany::too many emp");
			rp.setReturnCode(ReturnCodes.HAVE_JOIN_COMPANY.getCode());
			rp.setReturnMsg(ReturnCodes.HAVE_JOIN_COMPANY.getMessage());
			return rp;
		}
		EmployeeInfo emp = null;
		if(null == loyee){
			EmployeeInfo loyee2 = new EmployeeInfo();
			loyee2.setAtUserId(atUserId);
			loyee2.setCompanyId(companyId);
			loyee2.setAuditIsPassed("0");
			List<EmployeeInfo> list2 = employeeInfoDao.getEmployeeInfoByCondition(loyee2);
			if(null == list2 || list2.size() == 0){
				loyee2 = null;
			}else if(list2.size() == 1){
				loyee2 = list2.get(0);
			}else if(list2.size() > 1){
				logger.error("nsy-auth::joinCompany::join audited");
				rp.setReturnCode(ReturnCodes.JOIN_COMPANY_AUDITED.getCode());
				rp.setReturnMsg(ReturnCodes.JOIN_COMPANY_AUDITED.getMessage());
				return rp;
			}
			if(null == loyee2){
				emp = new EmployeeInfo();
				emp.setAtUserId(atUserId);
				emp.setRealName(realName);
				emp.setCompanyId(companyId);
				emp.setCompanyName(companyName);
				emp.setDepartmentId(departmentId);
				emp.setDepartmentName(departmentName);
				emp.setPosition(position);
				emp.setAuditIsPassed("0");
				emp.setEditTime(args.getEditTime());
				emp.setEditUserId(args.getEditUserId());
				emp.setValidity("1");
				emp.setOpAuthority("11");
				emp.setCreater("0");
				emp.setSyncTime(currtime);
				emp.setSyncType("0");
				emp.setCreateTime(currtime);
				employeeInfoDao.insertEmployeeInfo(emp);
				rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
				rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
			}else{
				rp.setReturnCode(ReturnCodes.JOIN_COMPANY_AUDITED.getCode());
				rp.setReturnMsg(ReturnCodes.JOIN_COMPANY_AUDITED.getMessage());
			}
		}else{
			logger.error("nsy-auth::joinCompany::joined emp");
			rp.setReturnCode(ReturnCodes.HAVE_JOIN_COMPANY.getCode());
			rp.setReturnMsg(ReturnCodes.HAVE_JOIN_COMPANY.getMessage());
		}
		return rp;
	}
	
	/**
	 * 加入公司审批
	 */
	@Transactional
	public ReturnPojo joinAuditPass(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String auditAtUserId = jb.getString("auditAtUserId");
		String companyId = jb.getString("companyId");
		String departmentId = jb.getString("departmentId");
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(auditAtUserId)
				|| StringUtils.isEmpty(companyId)
				|| StringUtils.isEmpty(departmentId)
				){
			logger.info("nsy-auth::joinCompany:: missing args: [atUserId,auditAtUserId,companyId,departmentId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		//判断被审核人员是否已经加入其他公司
		EmployeeInfo loyee = new EmployeeInfo();
		loyee.setAtUserId(auditAtUserId);
		loyee.setAuditIsPassed("1");
		List<EmployeeInfo> list = employeeInfoDao.getEmployeeInfoByCondition(loyee);
		if(null == list || list.size() == 0){
			loyee = null;
		}else if(list.size() == 1){
			loyee = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::joinCompany::too many emp");
			rp.setReturnCode(ReturnCodes.HAVE_JOIN_COMPANY.getCode());
			rp.setReturnMsg(ReturnCodes.HAVE_JOIN_COMPANY.getMessage());
			return rp;
		}
		EmployeeInfo emp = null;
		if(null == loyee){
			emp = new EmployeeInfo();
			emp.setAtUserId(auditAtUserId);
			emp.setCompanyId(companyId);
			emp.setDepartmentId(departmentId);
			emp.setAuditIsPassed("1");
			emp.setEditTime(args.getEditTime());
			emp.setEditUserId(args.getEditUserId());
			emp.setSyncTime(currtime);
			employeeInfoDao.updateEmployeeInfoByAudit(emp);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{
			logger.error("nsy-auth::joinAuditPass::audit not pass");
			rp.setReturnCode(ReturnCodes.JOIN_COMPANY_AUDIT_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.JOIN_COMPANY_AUDIT_FAIL.getMessage());
		}
		return rp;
	}
	
	/**
	 * 离开公司
	 */
	@Transactional
	public ReturnPojo exitCompany(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) 
				|| StringUtils.isEmpty(companyId)
				){
			logger.info("nsy-auth::joinCompany:: missing args: [atUserId,companyId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		EmployeeInfo emp = new EmployeeInfo();
		emp.setAtUserId(atUserId);
		emp.setCompanyId(companyId);
		emp.setEditTime(args.getEditTime());
		emp.setEditUserId(args.getEditUserId());
		emp.setSyncTime(currtime);
		employeeInfoDao.updateEmployeeInfoByExit(emp);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 查询组织架构
	 */
	public ReturnPojo queryOrganizaStruct(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String haveEmp = jb.getString("haveEmp");
		String haveJoin = jb.getString("haveJoin");
		if(StringUtils.isEmpty(atUserId) 
				|| StringUtils.isEmpty(companyId) || StringUtils.isEmpty(haveEmp)
				|| StringUtils.isEmpty(haveJoin) ){
			logger.info("nsy-auth::queryOrganizaStruct:: missing args: [atUserId,companyId,haveEmp,haveJoin]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		JSONObject jsons = new JSONObject();
		
		if("1".equals(haveEmp)){ 
			CompanyDepartment department = new CompanyDepartment();
			department.setCompanyId(companyId);
			department.setValidity("1");
			department.setType("1");
			List<CompanyDepartment> departments = departmentDao.getCompanyDepartmentByCondition(department);
			jsons.put("departments", departments);
			EmployeeInfo emp = new EmployeeInfo();
			emp.setCompanyId(companyId);
			if("1".equals(haveJoin)){
				emp.setAuditIsPassed("1");
			}
			List<EmployeeInfo> employees = employeeInfoDao.getEmployeeInfoByCondition(emp);
			jsons.put("employees", employees);
		}else if("2".equals(haveEmp)){
			CompanyDepartment department = new CompanyDepartment();
			department.setCompanyId(companyId);
			department.setValidity("1");
			department.setType("1");
			List<CompanyDepartment> departments = departmentDao.getCompanyDepartmentByCondition(department);
			jsons.put("departments", departments);
		}else if("3".equals(haveEmp)){
			EmployeeInfo emp = new EmployeeInfo();
			emp.setCompanyId(companyId);
			if("1".equals(haveJoin)){
				emp.setAuditIsPassed("1");
			}
			List<EmployeeInfo> employees = employeeInfoDao.getEmployeeInfoByCondition(emp);
			jsons.put("employees", employees);
		}else{
			logger.info("nsy-auth::queryOrganizaStruct:: haveEmp not value: [1,2,3]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		rp.setResult(jsons);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 添加部门xxxxxxxxxxxxxxxxxxx
	 */
	@Transactional
	public ReturnPojo addDepartment(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String companyName = jb.getString("companyName");
		String type = jb.getString("type");
		String fatherId = jb.getString("fatherId");
		String fatherName = jb.getString("fatherName");
		String sonDepart = jb.getString("sonDepart");
		String levelNum = jb.getString("levelNum");
		
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) || StringUtils.isEmpty(companyName) || StringUtils.isEmpty(type) 
				|| StringUtils.isEmpty(fatherId) || StringUtils.isEmpty(sonDepart) || StringUtils.isEmpty(levelNum) 
				|| StringUtils.isEmpty(fatherName) ){
			logger.info("nsy-auth::addDepartment:: missing args: [atUserId,companyId,type,fatherDepart,sonDepart,levelNum,remark]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		//判断同级别下是否同名部门
		CompanyDepartment department = new CompanyDepartment();
		department.setCompanyId(companyId);
		department.setCompanyName(companyName);
		department.setFatherId(fatherId);
		department.setFatherName(fatherName);
		department.setSonDepart(sonDepart);
		List<CompanyDepartment> list = departmentDao.getCompanyDepartmentByCondition(department);
		if(null == list || list.size() == 0){
			department= null;
		}else if(list.size() == 1){
			department = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::addDepartment::too many department");
			rp.setReturnCode(ReturnCodes.ADD_DEPARTMENT_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.ADD_DEPARTMENT_FAIL.getMessage());
			return rp;
		}
		if(null == department){
			String departmentId = Utils.makeDepartmentId();
			department = new CompanyDepartment();
			department.setDepartmentId(departmentId);
			department.setType(type);
			department.setFatherId(fatherId);
			department.setFatherName(fatherName);
			department.setSonDepart(sonDepart);
			department.setLevelNum(levelNum);
			department.setCompanyId(companyId);
			department.setCompanyName(companyName);
			department.setEditUserId(args.getEditUserId());
			department.setEditTime(args.getEditTime());
			department.setValidity("1");
			department.setSyncTime(currtime);
			department.setSyncType("0");
			department.setCreateTime(currtime);
			departmentDao.insertDepartment(department);
			CompanyDepartment cd = new CompanyDepartment();
			cd.setDepartmentId(departmentId);
			rp.setResult(cd);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{
			rp.setReturnCode(ReturnCodes.DEPARTMENT_EXISTED.getCode());
			rp.setReturnMsg(ReturnCodes.DEPARTMENT_EXISTED.getMessage());
		}
		return rp;
	}
	
	/**
	 * 删除部门
	 */
	@Transactional
	public ReturnPojo deleteDepartment(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String departmentId = jb.getString("departmentId");
		String currtime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) || StringUtils.isEmpty(departmentId) 
				){
			logger.info("nsy-auth::deleteDepartment:: missing args: [atUserId,companyId,departmentId]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		//物理删除部门，然后清除该部门下所有员工的关联关系。
//		CompanyDepartment department = new CompanyDepartment();
//		department.setDepartmentId(departmentId);
		Map<String, String> map = new HashMap<String, String>();
		map.put("departmentId", departmentId);
		map.put("companyId", companyId);
		map.put("editTime", args.getEditTime());
		map.put("editUserId", args.getEditUserId());
		map.put("syncTime", currtime);
		departmentDao.deleteCompanyDepartmentById(map);
//		EmployeeInfo employeeInfo = new EmployeeInfo();
//		employeeInfo.setCompanyId(companyId);
//		employeeInfo.setDepartmentId(departmentId);
//		employeeInfo.setEditTime(args.getEditTime());
//		employeeInfo.setEditUserId(args.getEditUserId());
//		employeeInfo.setSyncTime(currtime);
//		employeeInfoDao.clearEmployeeDepartment(employeeInfo);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	/**
	 * 部门重命名
	 */
	@Transactional
	public ReturnPojo renameDepartment(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = EncodingTool.encodeStr(args.getBusiness());
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String companyId = jb.getString("companyId");
		String fatherId = jb.getString("fatherId");
		String departmentId = jb.getString("departmentId");
		String departmentName = jb.getString("departmentName");
		String currtime = Utils.getCurrTimeMillions();
		String editTime = args.getEditTime();
		String editUserId = args.getEditUserId();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(companyId) || StringUtils.isEmpty(departmentId) 
				|| StringUtils.isEmpty(departmentName) || StringUtils.isEmpty(fatherId)){
			logger.info("nsy-auth::renameDepartment:: missing args: [atUserId,companyId,departmentId,departmentName]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		//判断同级别下是否同名部门,部门重命名，然后重命名原部门下所有员工的关联的部门名称
		CompanyDepartment department = new CompanyDepartment();
		department.setCompanyId(companyId);
		department.setFatherId(fatherId);
		department.setSonDepart(departmentName);
		List<CompanyDepartment> list = departmentDao.getCompanyDepartmentByCondition(department);
		if(null == list || list.size() == 0){
			department= null;
		}else if(list.size() == 1){
			department = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::renameDepartment::too many department");
			rp.setReturnCode(ReturnCodes.ADD_DEPARTMENT_FAIL.getCode());
			rp.setReturnMsg(ReturnCodes.ADD_DEPARTMENT_FAIL.getMessage());
			return rp;
		}
		if(null == department){
			department = new CompanyDepartment();
			department.setDepartmentId(departmentId);
			department.setSonDepart(departmentName);
			department.setEditTime(editTime);
			department.setEditUserId(editUserId);
			department.setSyncTime(currtime);
			departmentDao.updateDepartment(department);
			EmployeeInfo employeeInfo = new EmployeeInfo();
			employeeInfo.setCompanyId(companyId);
			employeeInfo.setDepartmentId(departmentId);
			employeeInfo.setDepartmentName(departmentName);
			employeeInfo.setEditTime(args.getEditTime());
			employeeInfo.setEditUserId(args.getEditUserId());
			employeeInfo.setSyncTime(currtime);
			employeeInfoDao.updateEmployeeDepartmentName(employeeInfo);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}else{
			rp.setReturnCode(ReturnCodes.DEPARTMENT_EXISTED.getCode());
			rp.setReturnMsg(ReturnCodes.DEPARTMENT_EXISTED.getMessage());
		}
		return rp;
	}
	
	/**
	 * 删除渠道
	 */
	@Transactional
	public ReturnPojo deleteRelations(ArgsPojo args) {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String channelIds = jb.getString("channelIds");
		String currTime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) 
				|| StringUtils.isEmpty(channelIds)){
			logger.info("nsy-auth::deleteRelations:: missing args: [atUserId,frontIds]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		List<CompanyRelationship> bases = new ArrayList<CompanyRelationship>();
		JSONArray frontIdArr = JSON.parseArray(channelIds);
		for(int j=0; j<frontIdArr.size(); j++) {
			JSONObject json = (JSONObject)frontIdArr.get(j);
			if(json == null) {
				logger.info("nsy-auth::deleteRelations::json transform error!");
				break;
			}
			CompanyRelationship relation = JSONObject.parseObject(json.toJSONString(), CompanyRelationship.class);
			relation.setValidity("0");
			relation.setSyncTime(currTime);
			bases.add(relation);
		}
		companyRelationshipDao.updateBatchCompanyRelationship(bases);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		return rp;
	}
	
	@Override
	@Transactional
	public ReturnPojo resetPwd(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String atUserId = jb.getString("atUserId");
		String pwd = jb.getString("pwd");
		String oldpwd = jb.getString("oldpwd");
		String currTime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(oldpwd)
				|| StringUtils.isEmpty(pwd)){
			logger.info("nsy-auth::resetPwd:: missing args: [atUserId,oldpwd,pwd]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		UserPassword up = new UserPassword();
		up.setAtUserId(atUserId);
		up.setUserPwd(oldpwd);
		List<UserPassword> list = userPasswordDao.getUserPasswordByCondition(up);
		if(null == list || list.size() == 0){
			up= null;
		}else if(list.size() == 1){
			up = list.get(0);
		}else if(list.size() > 1){
			logger.error("nsy-auth::resetPwd::too many password");
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.TOO_MANY_DATA.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
			return rp;
		}
		if(up == null){
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.NOT_EXISTS_PWD_FAIL.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}else{
			UserPassword password = new UserPassword();
			password.setAtUserId(atUserId);
			password.setUserPwd(pwd);
			password.setEditUserId(args.getEditUserId());
			password.setEditTime(args.getEditTime());
			password.setSyncTime(currTime);
			userPasswordDao.updateUserPassword(password);
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}
		return rp;
	}
	
	@Override
	@Transactional
	public ReturnPojo findPwd(ArgsPojo args) throws ApplicationException {
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ReturnCodes.UNKNOWN_EX.getCode());
		rp.setReturnMsg(ReturnCodes.UNKNOWN_EX.getMessage());
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		logger.info("nsy-auth::findPwd:: business is :: " + business);
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		
		String registerNum = jb.getString("registerNum");
		String pwd = jb.getString("pwd");
		String currTime = Utils.getCurrTimeMillions();
		if(StringUtils.isEmpty(registerNum)
				|| StringUtils.isEmpty(pwd)){
			logger.info("nsy-auth::findPwd:: missing args: [registerNum,pwd]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		UserList ul = new UserList();
		ul.setAtUserId(registerNum);
		UserList ac = userDao.getUserById(ul);
		logger.info("nsy-auth::findPwd:: user is :: " + ac);
		if(ac == null){
			ReturnCode rc = returnCodeDao.getReturnCodeByCode(ReturnCodes.NOT_HAVE_ACCOUNT.getCode());
			rp.setReturnCode(rc.getCode());
			rp.setReturnMsg(rc.getMessage());
		}else{
			UserPassword password = new UserPassword();
			password.setAtUserId(registerNum);
			password.setUserPwd(pwd);
			password.setEditUserId(args.getEditUserId());
			password.setEditTime(args.getEditTime());
			password.setSyncTime(currTime);
			userPasswordDao.updateUserPasswordByAtUserId(password);
			logger.info("nsy-auth::findPwd:: find pwd is OK ");
			rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
			rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		}
		return rp;
	}

//	private JSONArray treeMenuList(JSONArray departmentList, String parentId) {  
//       JSONArray childMenu = new JSONArray();
//       EmployeeInfo emp = new EmployeeInfo();
//       for (Object object : departmentList) {  
//           JSONObject jsonMenu = (JSONObject) JSON.toJSON(object);  
//           String menuId = jsonMenu.getString("sonDepart");  
//           String pid = jsonMenu.getString("fatherDepart");
//           String departmentId = jsonMenu.getString("departmentId");
//           if (parentId.equals(pid)) {  
//               JSONArray c_node = treeMenuList(departmentList, menuId);  
//               jsonMenu.put("childNode", c_node);
//               emp.setDepartmentId(departmentId);
//               employeeInfoDao.getEmployeeInfoByCondition(emp);
//               childMenu.add(jsonMenu);  
//           }  
//       }  
//       return childMenu;  
//   }
	

	/**
	 * 获取七牛token
	 */
	public ReturnPojo getUpLoadToken(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String bucket = jb.getString("bucket");
		if(StringUtils.isEmpty(bucket)  ){
			logger.info("nsy-auth::getUpLoadToken:: missing args: [bucket]");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		
		JSONObject rstJson = new JSONObject();
	    Auth auth = Auth.create(Constants.ACCESS_KEY, Constants.SECRET_KEY);
		String token = auth.uploadToken(bucket);
		rstJson.put("upLoadToken",token);
		logger.info("nsy-auth::getUpLoadToken:: get token::" + token);
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		rp.setResult(rstJson);
		return rp;
	}
	
	/**
	 * 获取七牛图片
	 */
	@Transactional
	public ReturnPojo downLoad(ArgsPojo args) throws Exception{
		ReturnPojo rp = new ReturnPojo();
		rp.setServiceCode(args.getServiceCode());
		rp.setReturnCode(ErrCode.UNKNOWN_EX);
		rp.setReturnMsg(ErrMsg.UNKNOWN_EX);
		rp.setCacheTime("0");
		rp.setCheckTime("0");
		String business = args.getBusiness();
		JSONObject jb = (JSONObject)JSONObject.parse(business);
		if(jb == null) {
			return rp;
		}
		String downloadURL = jb.getString("downloadURL");
		if(StringUtils.isEmpty(downloadURL)  ){
			logger.info("nsy-auth::resetUserInfo:: missing args: [downloadURL] ");
			rp.setReturnCode(ReturnCodes.BUSINESS_PARAM_NULL.getCode());
			rp.setReturnMsg(ReturnCodes.BUSINESS_PARAM_NULL.getMessage());
			return rp;
		}
		String qiNiuHost="http://www.supeffic.com/";
		JSONObject rstJson = new JSONObject();
	    JSONObject jsonObject = JSONObject.parseObject(business);
	    String downloadUrl=jsonObject.getString("downloadURL");
	    String url=qiNiuHost+downloadUrl;
	    Auth auth = Auth.create(Constants.ACCESS_KEY, Constants.SECRET_KEY);
	    String downLoad=auth.privateDownloadUrl(url, 3600 * 24*30);
	    
	    rstJson.put("downLoad", downLoad);
		
		logger.info("nsy-auth::resetUserInfo:: update user prop end");
		rp.setReturnCode(ReturnCodes.SUCCESS.getCode());
		rp.setReturnMsg(ReturnCodes.SUCCESS.getMessage());
		rp.setResult(rstJson);
		return rp;
	}

	@Override
	public ReturnCode getReturnCode(String code) throws ApplicationException {
		return returnCodeDao.getReturnCodeByCode(code);
	}
	
	/**
	 * ��QQ��
	 */
//	@Override
//	public ReturnPojo bindingQQ(ArgsPojo args) throws Exception {
//		logger.info("nsy-auth::bindingQQ::��QQ��");
//		ReturnPojo rp = new ReturnPojo();
//		rp.setServiceCode(args.getServiceCode());
//		rp.setReturnCode(ErrCode.UNKNOWN_EX);
//		rp.setReturnMsg("δ֪�쳣");
//		rp.setCacheTime("0");
//		rp.setCheckTime("0");
//		String atUserId = args.getAtUserId();
//		String qqNum = args.getQqNum();
//		String currTime = Utils.getCurrTime();
//		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(qqNum)){
//			rp.setReturnCode(ErrCode.PARAM_NULL);
//			rp.setReturnMsg("��������");
//		}else{
//			logger.info("nsy-auth::bindingQQ::getUserById:�ò�ѯ������Ϣ");
//			UserList ac = userDao.getUserById(atUserId);
//			String token = ac.getToken();
//			if(StringUtils.isEmpty(token)){
//				rp.setReturnCode(ErrCode.TOKEN_INVALID);
//				rp.setReturnMsg("tokenʧЧ");
//			}else{
//				String sign = null;
//				try {
//					sign = TokenUtils.getSign(token, args.getTimestamp(), args.getNonce());
//				} catch (NoSuchAlgorithmException e) {
//					logger.info("nsy-auth::bindingQQ::ǩ���쳣:" + e.getMessage());
//					rp.setReturnCode(ErrCode.SIGN_MAKE_FAIL);
//					rp.setReturnMsg("ǩ������ʧ��");
//					return rp;
//				}
//				
//				if(sign.equals(args.getSign())){
//					ac.setQqNum(qqNum);
//					boolean uflag = userDao.updateUser(ac) > 0 ? true : false;
//					if(uflag){
//						rp.setReturnCode(ErrCode.SUCCESS);
//						rp.setReturnMsg("��QQ�ųɹ�");
//					}else{
//						rp.setReturnCode(ErrCode.BINDING_QQ_FAIL);
//						rp.setReturnMsg("��QQ��ʧ��");
//					}
//				}else{
//					rp.setReturnCode(ErrCode.SIGN_VALID_FAIL);
//					rp.setReturnMsg("ǩ����֤ʧ��");
//				}
//				
//				try {
//					String notifyId = Utils.makeNotifyId();
//					args.setNotifyId(notifyId);
//					AccessNotify an = new AccessNotify();
//					an.setNotifyId(notifyId);
//					an.setUpdateTime(currTime);
//					an.setUpdateUser(args.getAtUserId());
//					boolean notifyFlag = userDao.insertNotify(an) > 0 ? true : false;
//					if(!notifyFlag){ //��֤ID����ʧ��
//						notifyId = "";
//					}else{
//						rp.setNotifyId(notifyId);
//					}
//				} catch (Exception e) {
//					logger.info("nsy-auth::bindingQQ::֪ͨ�쳣:" + e.getMessage());
//				}
//				
//			}
//		}
//		return rp;
//	}

	/**
	 * ��΢�ź�
	 */
//	@Override
//	public ReturnPojo bindingWeChat(ArgsPojo args) throws Exception {
//		logger.info("nsy-auth::bindingWeChat::��΢�ź�");
//		ReturnPojo rp = new ReturnPojo();
//		rp.setServiceCode(args.getServiceCode());
//		rp.setReturnCode(ErrCode.UNKNOWN_EX);
//		rp.setReturnMsg("δ֪�쳣");
//		rp.setCacheTime("0");
//		rp.setCheckTime("0");
//		String atUserId = args.getAtUserId();
//		String weChatNum = args.getWeChatNum();
//		String currTime = Utils.getCurrTime();
//		if(StringUtils.isEmpty(atUserId) || StringUtils.isEmpty(weChatNum)){
//			rp.setReturnCode(ErrCode.PARAM_NULL);
//			rp.setReturnMsg("��������");
//		}else{
//			logger.info("nsy-auth::bindingWeChat::getUserById:�ò�ѯ������Ϣ");
//			UserList ac = userDao.getUserById(atUserId);
//			String token = ac.getToken();
//			if(StringUtils.isEmpty(token)){
//				rp.setReturnCode(ErrCode.TOKEN_INVALID);
//				rp.setReturnMsg("tokenʧЧ");
//			}else{
//				String sign = null;
//				try {
//					sign = TokenUtils.getSign(token, args.getTimestamp(), args.getNonce());
//				} catch (NoSuchAlgorithmException e) {
//					logger.info("nsy-auth::bindingWeChat::ǩ���쳣:" + e.getMessage());
//					rp.setReturnCode(ErrCode.SIGN_MAKE_FAIL);
//					rp.setReturnMsg("ǩ������ʧ��");
//					return rp;
//				}
//				
//				if(sign.equals(args.getSign())){
//					ac.setWeChatNum(weChatNum);
//					boolean uflag = userDao.updateUser(ac) > 0 ? true : false;
//					if(uflag){
//						rp.setReturnCode(ErrCode.SUCCESS);
//						rp.setReturnMsg("��΢�źųɹ�");
//					}else{
//						rp.setReturnCode(ErrCode.BINDING_QQ_FAIL);
//						rp.setReturnMsg("��΢�ź�ʧ��");
//					}
//				}else{
//					rp.setReturnCode(ErrCode.SIGN_VALID_FAIL);
//					rp.setReturnMsg("ǩ����֤ʧ��");
//				}
//				
//				try {
//					String notifyId = Utils.makeNotifyId();
//					args.setNotifyId(notifyId);
//					AccessNotify an = new AccessNotify();
//					an.setNotifyId(notifyId);
//					an.setUpdateTime(currTime);
//					an.setUpdateUser(args.getAtUserId());
//					boolean notifyFlag = userDao.insertNotify(an) > 0 ? true : false;
//					if(!notifyFlag){ //��֤ID����ʧ��
//						notifyId = "";
//					}else{
//						rp.setNotifyId(notifyId);
//					}
//				} catch (Exception e) {
//					logger.info("nsy-auth::bindingWeChat::֪ͨ�쳣:" + e.getMessage());
//				}
//				
//			}
//		}
//		return rp;
//	}

}
